package user

import (
	"Web-Shop/models"
	"fmt"
	"strconv"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"github.com/gomodule/redigo/redis"
)

func (c *UserController) ShowCart() {
	if !c.IsLogin {
		c.Redirect("/login", 302)
	}
	conn, err := redis.Dial("tcp", "47.111.181.52:6379")
	if err != nil {
		beego.Info("redis链接失败")
		return
	}
	defer conn.Close()
	// 将hash转换成map
	goodsMap, _ := redis.IntMap(conn.Do("hgetall", "cart_"+strconv.Itoa(c.User.Id)))
	goods := make([]map[string]interface{}, len(goodsMap))
	fmt.Println(goodsMap)
	i := 0
	totalPrice := 0
	totalCount := 0
	for index, value := range goodsMap {
		skuid, _ := strconv.Atoi(index)
		// 根据hash的key获取到这种商品
		goodsSku := models.GoodsSKU{
			Id: skuid,
		}
		orm.NewOrm().Read(&goodsSku)

		// 创建一个map，用来存储商品种类及其数量，及其总价
		temp := make(map[string]interface{})
		temp["goodsSku"] = goodsSku
		temp["count"] = value

		totalPrice += goodsSku.Price * value
		totalCount += value

		temp["addPrice"] = goodsSku.Price * value
		goods[i] = temp
		i += 1
	}
	c.Data["totalPrice"] = totalPrice
	c.Data["totalCount"] = totalCount
	c.Data["goods"] = goods
	c.TplName = "cart.html"
}
func (c *UserController) AddCart() {
	if !c.IsLogin {
		c.Redirect("/login", 302)
	}
	skuid, err := c.GetInt("skuid")
	count, err := c.GetInt("count")
	resp := make(map[string]interface{})
	defer c.ServeJSON()
	if err != nil {
		resp["ok"] = false
		resp["msg"] = "传递的数据非法"
		c.Data["json"] = resp
		return
	}
	conn, err := redis.Dial("tcp", "47.111.181.52:6379")
	if err != nil {
		beego.Info("redis数据库连接错误")
		return
	}
	// 获取用户购物车某一种商品的数量
	preCount, err := redis.Int(conn.Do("hget", "cart_"+strconv.Itoa(c.User.Id), skuid))
	// 将新添加的这种商品的数量加上以往这种商品的数量
	conn.Do("hset", "cart_"+strconv.Itoa(c.User.Id), skuid, count+preCount)
	// 获取总的商品种类数量
	rep, err := conn.Do("hlen", "cart_"+strconv.Itoa(c.User.Id))
	cartCount, _ := redis.Int(rep, err)
	resp["ok"] = true
	resp["cartCount"] = cartCount
	c.Data["json"] = resp
}

func (c *UserController) DeleteCart() {
	if !c.IsLogin {
		c.Redirect("/login", 302)
	}
	skuid, err := c.GetInt("skuid")
	resp := make(map[string]interface{})
	defer c.ServeJSON()

	if err != nil {
		resp["ok"] = false
		resp["errmsg"] = "请求数据非法"

		c.Data["json"] = resp
		return
	}
	conn, err := redis.Dial("tcp", "47.111.181.52:6379")
	if err != nil {
		resp["ok"] = false
		resp["msg"] = "服务器错误"

		c.Data["json"] = resp
		return
	}
	defer conn.Close()

	conn.Do("hdel", "cart_"+strconv.Itoa(c.User.Id), skuid)

	resp["ok"] = true
	resp["msg"] = "ok"
	c.Data["json"] = resp
}

func (c *UserController) UpdateCart() {
	skuid, err := c.GetInt("skuid")
	count, err := c.GetInt("count")
	resp := make(map[string]interface{})
	defer c.ServeJSON()

	if err != nil {
		resp["ok"] = false
		resp["errmsg"] = "请求数据非法"
		c.Data["json"] = resp
		return
	}

	conn, err := redis.Dial("tcp", "47.111.181.52:6379")
	if err != nil {
		resp["ok"] = false
		resp["msg"] = "服务器错误"

		c.Data["json"] = resp
		return
	}
	defer conn.Close()
	conn.Do("hset", "cart_"+strconv.Itoa(c.User.Id), skuid, count)
	resp["ok"] = true
	resp["msg"] = "ok"

	c.Data["json"] = resp
}
